<?php defined('SYSPATH') or die('No direct script access.');

/**
 * @file term.php
 * @brief 类型管理
 * @author Lionel Tsai
 * @version 1.0
 * @date 2012-02-17
 */
class Controller_Term extends Controller_Rest {
	public function before()
	{
		parent::before();
	}
	/**
	 * @brief 获取term
	 *
	 * @param $term_id
	 *
	 * @return $this
	 */
	public function action_get($term_id = NULL)
	{
		if($term_id)
		{
			$this->get_term($term_id);
		}
		else
		{
			$this->get_terms();
		}
	}
	/**
	 * @brief 获取指定ID的类型
	 *
	 * @param $term_id
	 *
	 * @return $this 
	 */
	private function get_term($term_id)
	{
		$term = ORM::factory('term', $term_id);
		if(!$term->term_id)
		{
			$this->response->body('类型不存!')->status(206);
			return;
		}
		$res = json_encode($term->as_array());
		$this->response->body($res)->status(200);
	}
	/**
	 * @brief 获取指定起止位置的类型
	 *
	 * @return $this 
	 */
	private function get_terms()
	{
		/** 获取起止数*/
		$start = filter_input(INPUT_GET, 'start', FILTER_VALIDATE_INT);
		$pos = filter_input(INPUT_GET, 'pos', FILTER_VALIDATE_INT);

		$term = ORM::factory('term');
	
		$start = ($start) ? $start : 0;
		$pos = ($pos) ? $pos : PHP_INT_MAX;
		
		$terms = $term
				->offset($start)
				->limit($pos)
				->find_all();
		if(!$terms->count())
		{
			$this->response->body('类型不存在！')->status(206);
		}
		else
		{
			/** 返回条数及列表*/
			$res['num'] = $terms->count();
			$res['list'] = array();
			foreach($terms as $tem)
			{
				$res['list'][] = $tem->as_array();
			}
			$json_res = json_encode($res);
			$this->response->body($json_res)->status(200);
		}
	}
	/**
	 * @brief 新增类型
	 *
	 * @return $this
	 */
	public function action_post()
	{
		$term=ORM::factory('term');
		if(!$_POST) return;
		if(isset($_POST['id']))
		{
			unset($_POST['id']);
		}

		try 
		{
			/** 提交term*/
			$term->values($_POST)->check();

			$term->save();
			$this->response->body('添加类型成功！')->status(200);
		}
		catch (ORM_Validation_Exception $e)
		{
			$errors = $e->errors();
			foreach($errors as $k => $v)
			{
				$this->response->body($k.':not available')->status(202);
			}
		}
	}
	/**
	 * @brief 更新指定类型
	 *
	 * @param $term_id
	 *
	 * @return $this
	 */
	public function action_put($term_id)
	{

		$term = ORM::factory('term', $term_id);
		if(!$term->term_id)
		{
			$this->response->body('类型不存在!')->status(206);
		}
		try 
		{
			$term->values($this->request->post())->check()->update();
			$this->response->body('更新类型成功！')->status(200);
		}
		catch (ORM_Validation_Exception $e) 
		{/** 验证错误*/
  		    $error = $e->errors();
			foreach($error as $k=>$v)
			{
				$this->response->body($k.':not available')->status(206);
			}
		}
		catch (Exception $err) 
		{/** 其他错误*/
            $this->response->body($err->getMessage())->status(206);
		}
	}
	/**
	 * @brief 删除指定类型
	 *
	 * @param $term_id
	 *
	 * @return	$this 
	 */
	public function action_delete($term_id)
	{
		$term = ORM::factory('term', $term_id);
		if(!$term->term_id)
		{
			$this->response->body('类型不存在！')->status(206);
			return;
		}
		try
		{
			$term->delete();
			$this->response->body('删除类型成功！')->status(200);
		}
		catch (Exception $err) 
		{
			$this->response->body($err->getMessage())->status(500);
		}
	}
}
